﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.FeatureBase;
using QLBH.Model.Controller;
using QLBH.DataModel;
using QLBH.ModelInterface;
using QLBH.ModelFactory;
using QLBH.Base;
using QLBH.StandardUI;

namespace QLBH.CongNo
{
    public partial class PhieuThuChiView : BaseView
    {
        private bool loaiPhieu = false;
        private List<IPhieuThuChiModel> datasource;
        public PhieuThuChiView()
            :base()
        {            
        }
        public PhieuThuChiView(bool loaiPhieu)
            : base()
        {
            this.loaiPhieu = loaiPhieu;
            datasource = new List<IPhieuThuChiModel>();
        }        
        /// <summary>
        /// Add new item event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void AddNew(object sender, EventArgs e)
        {
            PhieuThuChiDlg addNewDlg = new PhieuThuChiDlg(loaiPhieu);
            if (addNewDlg.ShowDialog()==DialogResult.OK)
            {
                RefreshData(this, null);
            }
            base.AddNew(sender, e);
        }
        /// <summary>
        /// edit item event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void EditItem(object sender, EventArgs e)
        {
            PhieuThuChiDlg editItemDlg = new PhieuThuChiDlg(loaiPhieu);
            editItemDlg.DlgType = eDlgType.EDIT;
            editItemDlg.Model = DataGridView.SelectedRows[0].DataBoundItem as IPhieuThuChiModel;
            if (editItemDlg.ShowDialog() == DialogResult.OK)
            {
                RefreshData(this, null);
            }
            base.EditItem(sender, e);
        }
        /// <summary>
        /// delete item event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DeleteItem(object sender, EventArgs e)
        {
            try
            {
                if (DataGridView.SelectedRows.Count > 0)
                {
                    IPhieuThuChiModel selectedItem = DataGridView.SelectedRows[0].DataBoundItem as IPhieuThuChiModel;
                    PhieuThuChiController.GetInstance().Delete(selectedItem.ToEntity());
                }
                base.DeleteItem(sender, e);

                RefreshData(this,null);
            }
            catch (System.Exception ex)
            {
                ShowMessage.Instance.ShowError(ex.Message);
            }
            
        }
        /// <summary>
        /// Init layout for data grid view
        /// </summary>
        public override void InitDataGridView()
        {
            //Init columns display for data grid view
            

            DataGridViewTextBoxColumn SOPHIEU = new DataGridViewTextBoxColumn();
            SOPHIEU.DataPropertyName = "SOPHIEU";
            SOPHIEU.HeaderText = "Số phiếu";
            SOPHIEU.Name = "sophieu";
            SOPHIEU.ReadOnly = true;
            SOPHIEU.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            SOPHIEU.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            SOPHIEU.MinimumWidth = 150;
            SOPHIEU.Visible = true;

            //MapHieu
            DataGridViewTextBoxColumn maphieu = new DataGridViewTextBoxColumn();
            maphieu.DataPropertyName = "MAPHIEU";
            maphieu.HeaderText = "Mã phiếu";
            maphieu.Name = "maphieu";
            maphieu.ReadOnly = true;
            maphieu.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            maphieu.MinimumWidth = 88;
            maphieu.Visible = false;

            DataGridViewTextBoxColumn TONGTIEN = new DataGridViewTextBoxColumn();
            TONGTIEN.DataPropertyName = "TONGTIEN";
            TONGTIEN.HeaderText = "Tổng tiền(VNĐ)";
            TONGTIEN.Name = "tongtien";
            TONGTIEN.ReadOnly = true;
            TONGTIEN.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            TONGTIEN.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            TONGTIEN.MinimumWidth = 150;
            TONGTIEN.Visible = true;


            DataGridViewTextBoxColumn malydo = new DataGridViewTextBoxColumn();
            malydo.DataPropertyName = "malydo";
            malydo.HeaderText = "malydo";
            malydo.Name = "malydo";
            malydo.ReadOnly = true;
            malydo.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            malydo.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            malydo.MinimumWidth = 88;
            malydo.Visible = false;


            DataGridViewTextBoxColumn TENLYDO = new DataGridViewTextBoxColumn();
            TENLYDO.DataPropertyName = "TENLYDO";
            TENLYDO.HeaderText = "Lý do";
            TENLYDO.Name = "TENLYDO";
            TENLYDO.ReadOnly = true;
            TENLYDO.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            TENLYDO.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            TENLYDO.MinimumWidth = 150;
            TENLYDO.Visible = true;

            DataGridViewTextBoxColumn NGAYTAO = new DataGridViewTextBoxColumn();
            NGAYTAO.DataPropertyName = "NGAYTAO";
            NGAYTAO.HeaderText = "Ngày tạo";
            NGAYTAO.Name = "ngaytao";
            NGAYTAO.ReadOnly = true;
            NGAYTAO.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            NGAYTAO.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            NGAYTAO.MinimumWidth = 150;
            NGAYTAO.Visible = true;

            DataGridViewCheckBoxColumn LOAIPHIEU = new DataGridViewCheckBoxColumn();
            LOAIPHIEU.DataPropertyName = "LOAIPHIEU";
            LOAIPHIEU.HeaderText = "loaiphieu";
            LOAIPHIEU.Name = "loaiphieu";
            LOAIPHIEU.ReadOnly = true;
            LOAIPHIEU.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            LOAIPHIEU.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            LOAIPHIEU.MinimumWidth = 88;
            LOAIPHIEU.Visible = false;

            DataGridViewTextBoxColumn GHICHU = new DataGridViewTextBoxColumn();
            GHICHU.DataPropertyName = "GHICHU";
            GHICHU.HeaderText = "Ghi chú";
            GHICHU.Name = "GHICHU";
            GHICHU.ReadOnly = true;
            GHICHU.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            GHICHU.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            GHICHU.MinimumWidth = 200;
            GHICHU.Visible = true;

            DataGridViewTextBoxColumn USERID = new DataGridViewTextBoxColumn();
            USERID.DataPropertyName = "USERID";
            USERID.HeaderText = "USERID";
            USERID.Name = "USERID";
            USERID.ReadOnly = true;
            USERID.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            USERID.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            USERID.MinimumWidth = 88;
            USERID.Visible = false;

            DataGridViewTextBoxColumn USERNAME = new DataGridViewTextBoxColumn();
            USERNAME.DataPropertyName = "USERNAME";
            USERNAME.HeaderText = "Người tạo";
            USERNAME.Name = "nguoitao";
            USERNAME.ReadOnly = true;
            USERNAME.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            USERNAME.Resizable = System.Windows.Forms.DataGridViewTriState.True;
            USERNAME.MinimumWidth = 200; 
            USERNAME.Visible = true;


            DataGridView.Columns.AddRange(new DataGridViewColumn[]
                {
                    maphieu,
                    SOPHIEU,
                    TONGTIEN,
                    malydo,
                    TENLYDO,
                    NGAYTAO,
                    LOAIPHIEU,
                    GHICHU,
                    USERID,
                    USERNAME
                });

            DataGridView.Columns["maphieu"].Visible = false;

            base.InitDataGridView();
        }
        /// <summary>
        /// Build search menu item(s)
        /// </summary>
        public override void BuildSearchMenu()
        {
            InvisibleSearchControl();

            base.BuildSearchMenu();
        }
        /// <summary>
        /// Refresh data on form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void RefreshData(object sender, EventArgs e)
        {
            datasource.Clear();
            datasource = new List<IPhieuThuChiModel>();
            //Load data to view
            List<PHIEUTHUCHI> phieuthuchis= PhieuThuChiController.GetInstance().FindBy(
                ptc=> ptc.LOAIPHIEU==loaiPhieu).ToList();            

            foreach (PHIEUTHUCHI ptc in phieuthuchis)
            {
                IPhieuThuChiModel model = CongNoModelFactory.GetPhieuThuChiModelInstance();
                model.InitFromEntity(ptc);
                datasource.Add(model);
            }

            DataGridView.DataSource = datasource;

            base.RefreshData(sender, e);
        }
        /// <summary>
        /// Loaded form event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void LoadForm(object sender, EventArgs e)
        {
            RefreshData(sender, null);
            base.LoadForm(sender, e);
        }
        /// <summary>
        /// Datasource change event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void DataSourceChange(object sender, EventArgs e)
        {
            base.DataSourceChange(sender, e);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // PhieuThuChiView
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "PhieuThuChiView";
            this.Size = new System.Drawing.Size(1502, 872);
            this.ResumeLayout(false);

        }
        /// <summary>
        /// The event occur when any key down on form
        /// </summary>
        /// <param name="e">The Key event argument</param>
        public override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Control && e.KeyCode==Keys.F)
            {
                SearchPerform(null, null);
            }
            base.OnKeyDown(e);
        }
        /// <summary>
        /// invoke to Search handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void SearchPerform(object sender, EventArgs e)
        {
            FindPhieuThuChiDlg findDlg = new FindPhieuThuChiDlg();
            if (findDlg.ShowDialog()==DialogResult.OK)
            {
                List<IPhieuThuChiModel> temp = new List<IPhieuThuChiModel>();
                FindArg arg=findDlg.FindArgs;
                // to find process
                temp = datasource.Where(pc => 
                    pc.NGAYTAO >= arg.Fromdate && pc.NGAYTAO <= arg.ToDate).ToList();
 
                if (arg.LydoID!=Guid.Empty)
                {
                    temp = temp.Where(pc => 
                    pc.MALYDO == arg.LydoID).ToList(); 
                }                
                if (arg.UserID!=Guid.Empty)
                {
                    temp = temp.Where(pc =>
                    pc.USERID == arg.UserID).ToList(); 
                }
                DataGridView.DataSource = temp;
            }
            base.SearchPerform(sender, e);
        }

    }
}
